博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 读取 RSS,顺便还有xpath的用法
阅读量:4120 次
发布时间:2019-05-25

本文共 4933 字,大约阅读时间需要 16 分钟。

Java读取RSS:
  1. //http://zfive.blogbus.com/logs/20959998.html
  2. package com.yourcompany.struts.user;
  3. import java.io.IOException;
  4. import java.io.File;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.NodeList;
  14. import org.xml.sax.SAXException;
  15. import java.net.URL;
  16. public class Test {
  17.  public static void main(String[] args) {
  18.   try{
  19.    List result = new ArrayList();  //声明一个结果集
  20.    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //解析器工厂类
  21.    DocumentBuilder db;
  22.    Document doc;
  23.   
  24.    //此DocumentBuilder对象的作用是根据xml文件的url地址生成document对象
  25.    db =dbf.newDocumentBuilder();     //得到一个DOM解析器对象;
  26.     //File file = new File("students.xml"); //当然,我们也可以读出本地计算机中的xml文档
  27.   
  28.    //doc对象包含需要解析的feed的xml文件
  29.    URL file = new URL("http://localhost:8080/rSS/pfocus.xml");
  30.    
  31.    doc = db.parse(file.openStream());  //把解析后的XML,赋给文档对象; 
  32.    //feed的xml文件的组成单位是item,从doc中取出所有的item
  33.    NodeList nl = doc.getElementsByTagName("item");  //多个<item>标签组成一个链表;
  34.  
  35.    for(int i=0;i<nl.getLength();i++)
  36.    {
  37.     Element eltStu = (Element)nl.item(i);
  38.     //此处得到元素中的<title></title>标签实体;
  39.     Node titlenode = eltStu.getElementsByTagName("title").item(0);  
  40.     //返回第一个节点的值;
  41.       String title = titlenode.getFirstChild().getNodeValue();
  42.     
  43.     Node linknode = eltStu.getElementsByTagName("link").item(0);
  44.     String link = linknode.getFirstChild().getNodeValue();
  45.     
  46.     Node descriptionnode = eltStu.getElementsByTagName("description").item(0);
  47.     String description = descriptionnode.getFirstChild().getNodeValue();
  48.     
  49.     Node authornode = eltStu.getElementsByTagName("author").item(0);
  50.     String author = authornode.getFirstChild().getNodeValue();
  51.     
  52.     
  53.     System.out.println(title);
  54.     System.out.println(author);
  55.     System.out.println(link);
  56.     System.out.println(description);
  57.     System.out.println("---------");
  58.       }
  59.   }
  60.    catch(ParserConfigurationException e)
  61.    {
  62.     e.printStackTrace();
  63.    }catch(SAXException e){
  64.     e.printStackTrace();
  65.    }catch(IOException e){
  66.     e.printStackTrace();
  67.    }finally{
  68.    }
  69. }
  70. }
然后是xpath的部分:
 
import
 java.io.IOException;
 4 
 5 
import
 javax.xml.parsers.
*
;
 6 
import
 javax.xml.xpath.
*
;
 7 
 8 
import
 org.w3c.dom.
*
;
 9 
import
 org.xml.sax.SAXException;
10 
11 
class
 XpathSample {
12 
    
public
 
static
 
void
 main(String[] args) 
throws
 ParserConfigurationException,
13 
            SAXException, IOException, XPathExpressionException {
14 
        DocumentBuilderFactory factory 
=
 DocumentBuilderFactory.newInstance();
15 
        factory.setNamespaceAware(
true
);
16 
        DocumentBuilder builder 
=
 factory.newDocumentBuilder();
17 
        Document doc 
=
 builder.parse(
"
XmlFile/books.xml
"
);
18 
        XPathFactory xFactory 
=
 XPathFactory.newInstance();
19 
        XPath xpath 
=
 xFactory.newXPath();
20 
        XPathExpression expr 
=
 xpath
21 
                .compile(
"
//book[author='Neal Stephenson']/price/text()
"
);
22 
        Object result 
=
 expr.evaluate(doc, XPathConstants.NODESET);
23 
        NodeList nodes 
=
 (NodeList) result;
24 
        
for
 (
int
 i 
=
 
0
; i 
<
 nodes.getLength(); i
++
) {
25 
            System.out.println(nodes.item(i).getNodeValue());
26 
        }
27 
    }
28 
29 
}
大概就是这样了。对啦对啦,还有yahoo的weather格式:

首先了解Yahoo Weather Api的RSS Response格式:

http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="
http://www.w3.org/2003/01/geo/wgs84_pos#"> 
   
Yahoo! Weather - Tangshan, CH   
http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html   
Yahoo! Weather for Tangshan, CH
   
en-us
   
Fri, 22 Aug 2008 8:00 am CST
   
60
   
   
   
   
   
   
     
Yahoo! Weather     
142
     
18
     
http://weather.yahoo.com     
http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
       
     
Conditions for Tangshan, CH at 8:00 am CST     
39.63
     
118.17
     
http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html     
Fri, 22 Aug 2008 8:00 am CST
     
     
        http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/> />Current Conditions:
Mostly Cloudy, 67 F

Forecast:
Fri - Partly Cloudy. High: 86 Low: 69
Sat - Partly Cloudy. High: 88 Low: 70

Full">http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>     
     
     
     
CHXX0131_2008_08_22_8_00_CST
   
 

我们所需要用到的Node是//rss/channel/item/yweather:forecast

转载地址:http://zwipi.baihongyu.com/

你可能感兴趣的文章
java中Class对象详解
查看>>
Tomcat服务器性能优化
查看>>
ThreadLocal源码分析
查看>>
Dom4j使用Xpath语法读取xml节点
查看>>
解决程序启动时mysql出现 unauthenticated user导致数据库处理慢的问题
查看>>
OpenJDK和JDK区别
查看>>
漫谈REST架构风格
查看>>
编辑文件无法保存Fsync failed
查看>>
分布式版本控制工具Git
查看>>
Apache与Tomcat 区别联系
查看>>
ByteBuffer用法小结
查看>>
使用ViewBuffer操作ByteBuffer
查看>>
elasticsearch如何高效的使用filter
查看>>
AES加密CBC模式兼容互通四种编程语言平台【PHP、Javascript、Java、C#】
查看>>
各类题目答疑总结
查看>>
深度剖析Redis持久化
查看>>
Java NIO原理 图文分析及代码实现
查看>>
Spring AOP介绍及源码分析
查看>>
MySQL事务隔离级别详解
查看>>
常见事务并发问题以及处理方法
查看>>